﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Distributor_sxqd, GoPage, $timeout) {
		
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_data();
		}, false)
	
		//初始化页面
		$scope.init_data = function() {
			$scope.firstHide = false;
			Distributor_sxqd.distributor_search(1,function(data){
				$scope.datas = data;
				$scope.firstHide = true;
			})
		}
		
		
		/****************上下拉刷新****************/
		$scope.isLoad = false; //可以加载
		$scope.nPage = 1;
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Distributor_sxqd.distributor_search(1, function(data) {
					$scope.datas = data;
					$scope.nPage = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Distributor_sxqd.distributor_search($scope.nPage + 1, function(data) {
				if(data != null) {
					if($scope.datas.data != undefined) {
						if($scope.count!=null)
						$scope.datas.data = $scope.datas.data.concat(data.bank);
						$scope.nPage += 1;
					} else {
						$scope.datas = data;
					}
				}
			},function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		/****************上下拉刷新****************/
		
		//跳转到首页
		$scope.onshow_first = function($event,store_id) {
			$event.stopPropagation();//阻止事件冒泡
			GoPage.go_states("first", {
				"store_id": store_id
			});
		}
		//跳转供应商详情
		$scope.onshow_detail = function(store_id) {
			GoPage.go_states("distributor_sxqd_detail",{
				"store_id":store_id
			});
		}
		
		//授信欠款
		$scope.show_repayment=function(store_id){
			GoPage.go_states("credit_repayment",{
				"id":store_id
			});			
		}
		
		//发红包
		$scope.show_redbag=function(store_id){
			GoPage.go_states("distributor_redbag",{
				"store_id":store_id,
				'advance':$scope.datas.member_advance
			});			
		}
		
		
	}

	controller.$inject = ['$scope', 'Distributor_sxqd', 'GoPage','$timeout'];

	return app.registerController("distributor_sxqdctrl", controller);
});